#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    char input[1024];
    char *args[128]; 
    pid_t pid;
    while (1) {
        printf("myshell> ");
        fflush(stdout);
        if (!fgets(input, 1024, stdin)) {
            perror("fgets");
            break;
        }
		 if (strcmp(input, "exit\n") == 0) {
            break;
        }
        input[strlen(input) - 1] = '\0';
        pid = fork();
        if (pid < 0) {
            perror("fork");
            exit(1);
        } else if (pid == 0) {
         	int arg_count = 0;
        	char *token = strtok(input, " \n");
        while (token != NULL && arg_count < 127) {
            args[arg_count++] = token;
            token = strtok(NULL, " \n");
        }
        args[arg_count] = NULL;
        if (execvp(args[0], args) == -1) {
            perror("execvp");
            exit(1);
        }
        }
         else {
            wait(NULL);
        }
    }
    return 0;
}

